﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;


namespace 批量替换
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



















        /// <summary>
        /// 替换值
        /// </summary>
        /// <param name="strFilePath">txt等文件的路径</param>
        /// <param name="strIndex">索引的字符串，定位到某一行</param>
        /// <param name="newValue">替换新值</param>
        private void ReplaceValue(string strFilePath, string strIndex, string newValue)
        {
            if (File.Exists(strFilePath))
            {
                string[] lines = System.IO.File.ReadAllLines(strFilePath);
                for (int i = 0; i < lines.Length; i++)
                {
                    if (lines[i].Contains(strIndex))
                    {
                        string[] str = lines[i].Split('=');
                        str[1] = newValue;
                        lines[i] = str[0] + " = " + str[1];
                    }
                }
                File.WriteAllLines(strFilePath, lines);
            }
        }





        int num = 0;
        private void ReplaceValueByDictionary(string strFilePath, Dictionary<string, string> keyValuePairs,string L,string R)
        {
            if (File.Exists(strFilePath))
            {
                string[] lines = System.IO.File.ReadAllLines(strFilePath);
                for (int i = 0; i < lines.Length; i++)
                {
                    foreach (var key in keyValuePairs.Keys)
                    {


                        string old = L + key + R;


                        if (lines[i].Contains(old))
                        {
                            string newstr = old;
                            string oldline = lines[i];
                            bool f = keyValuePairs.TryGetValue(key, out newstr);
                            Console.WriteLine(f);
                            newstr = L + newstr + R;
                            string newline = oldline.Replace(old, newstr);
                            num++;
                            label4.Text = $"正在替换第{ num }处";
                            lines[i] = newline;
                        }
                    }

                }
                File.WriteAllLines(strFilePath, lines);
            }
        }


        Dictionary<string, string> GetDictionaryByText(string text)
        {

            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            string[] keyValues = textBox2.Text.Trim().Split(new string[] { "\r\n" }, StringSplitOptions.None);



            foreach (var keyValue in keyValues)
            {

                string[] s = keyValue.Split('@');
                if (s.Length != 2)
                {
                    MessageBox.Show("字典存入数据有问题" + s[0]);
                    return null;
                }
                if (!keyValuePairs.Keys.Contains(s[0]))
                {
                    keyValuePairs.Add(s[0], s[1]);
                }

            }

            keyValuePairs = keyValuePairs.OrderByDescending(p => p.Key.Length).ToDictionary(p => p.Key, o => o.Value);
            return keyValuePairs;
        }





        private void button1_Click(object sender, EventArgs e)
        {
            num = 0;
            string[] paths = textBox1.Text.Trim().Split(new string[] { "\r\n" }, StringSplitOptions.None);
            //string[] paths = textBox1.Lines;



            if (oDictionary != null && oDictionary.Count != 0)
            {
                foreach (var path in paths)
                {
                    ReplaceValueByDictionary(path, oDictionary,textBox_L.Text, textBox_R.Text);

                }
                label4.Text = $"替换完成，共计{ num }处";

            }
            else
            {
                MessageBox.Show("字典空");
            }


        }
        Dictionary<string, string> oDictionary = new Dictionary<string, string>();
        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox2.Text.Trim()))
            {
                oDictionary = GetDictionaryByText(textBox2.Text.Trim());
                label3.Text = oDictionary.Count.ToString() + "对";
            }

        }

        private void textBox_LR_TextChanged(object sender, EventArgs e)
        {
            label_show.Text = $"{textBox_L.Text}old{textBox_R.Text}→{textBox_L.Text}new{textBox_R.Text}";
        }
    }
}
